﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApp.data;
using System.Windows.Controls.DataVisualization.Charting;
using SilverlightApp.Common;
using System.Windows.Threading;

namespace SilverlightApp.Web
{
    public abstract class ColumnChartBuilderBase
    {
        private DispatcherTimer warningDispatcherTimer = null;
        private Chart ChartTemp = null;
        protected void BuildChartCore(Panel panel, ColumnChartBuildContext context)
        {
            Chart chart = new Chart { Title = context.ChartTitle };
            ChartTemp = chart;
            if (!context.ShowLegend)
            {
                Style legendStyle = new System.Windows.Style();
                Setter legendWidth = new Setter(System.Windows.Controls.Control.WidthProperty, 0);
                Setter legendHeight = new Setter(System.Windows.Controls.Control.HeightProperty, 0);
                legendStyle.Setters.Add(legendWidth);
                legendStyle.Setters.Add(legendHeight);
                legendStyle.TargetType = typeof(System.Windows.Controls.Control);
                chart.LegendStyle = legendStyle;
            }
            ColumnSeries series = new ColumnSeries();
            series.ItemsSource = context.DataSource;
            series.IndependentValueBinding = new System.Windows.Data.Binding("Name");
            series.DependentValueBinding = new System.Windows.Data.Binding("Value");
            if (context.SeriesDataPointStyle != null)
            {
                series.DataPointStyle = context.SeriesDataPointStyle;
            }
            else if (context.ColumnColor.HasValue)
            {
                Style lineStyle = new System.Windows.Style();
                lineStyle.TargetType = typeof(System.Windows.Controls.Control);
                Setter setter = new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(context.ColumnColor.Value));
                lineStyle.Setters.Add(setter);
                series.DataPointStyle = lineStyle;
            }
            AfterSeriesBuild(series);

            LinearAxis valueAxis = new LinearAxis { Orientation = AxisOrientation.Y, Minimum = 0, Maximum = 100, ShowGridLines = true };
            AfterLinearAxisBuild(valueAxis);
            if (context.WarnContext != null)
            {
                warningDispatcherTimer = new DispatcherTimer();
                warningDispatcherTimer.Interval = 
                    context.WarnContext.WarningInterval.HasValue ? TimeSpan.FromSeconds(0.3) : context.WarnContext.WarningInterval.Value;
            }
            context.DataSource.AfterPropertyChanged = (pi) =>
            {
                if (context.AfterPropertyChangedEnventHandler != null)
                {
                    context.AfterPropertyChangedEnventHandler(chart, pi);
                }
                if (context.WarnContext != null)
                {
                    if (context.WarnContext.RunWarnFilter == null)
                    {
                        throw new Exception();
                    }

                    if (context.WarnContext.RunWarnFilter(pi))
                    {
                        if (context.WarnContext.OnOdd != null && context.WarnContext.OnEven != null)
                        {
                        }
                        BeginWarning();
                    }

                }
            };
            AfterChartBuild(chart);

            chart.Axes.Add(valueAxis);
            chart.Series.Add(series);
            Grid grid = new Grid();
            grid.Children.Add(chart);
            panel.Children.Add(grid);
        }

        private void BeginWarning()
        {
            if (warningDispatcherTimer.IsEnabled)
            {
                return;
            }
            warningDispatcherTimer.Start();
        }

        protected virtual void AfterSeriesBuild(ColumnSeries series)
        {
        }
        protected virtual void AfterChartBuild(Chart chart)
        {
        }
        protected virtual void AfterLinearAxisBuild(LinearAxis la)
        {
        }
        public abstract void BuildChart(Panel panel, MyObservableCollection<PairInfo> ds);
    }

    public class ColumnChartBuildContext
    {
        //public double? WarningMinValue { get; set; }
        //public double? WarningMaxValue { get; set; }
        //public TimeSpan? WarningInterval { get; set; }
        public bool ShowLegend { get; set; }
        public object LegendTitle { get; set; }
        //  public EventHandler WarnEventHandler { get; set; }
        public Action<Chart, MyObservableCollection<PairInfo>> AfterPropertyChangedEnventHandler { get; set; }
        public object ChartTitle { get; set; }
        public MyObservableCollection<PairInfo> DataSource { get; set; }
        public Style SeriesDataPointStyle { get; set; }
        public Color? ColumnColor { get; set; }
        public RunWarningContext WarnContext { get; set; }
    }

    public class RunWarningContext
    {
        public Func<MyObservableCollection<PairInfo>, bool> RunWarnFilter;
        public Action<Chart, double> OnOdd { get; set; }
        public Action<Chart, double> OnEven { get; set; }
        public Color OddColor { get; set; }
        public Color EvenColor { get; set; }
        public TimeSpan? WarningInterval { get; set; }
    }
}
